/**
 * 文章预览
 * date: 2011-1-4
 */
//COMM.lib.load("editor");

(function($){
	
	ArticlePreview = {
		articleId : null,
		init : function(){
			this.articleId = $("#articleId").val();
		}
	};
	
	ArticlePreview.AddDetail = {
		editorId : 'addDetailContent',	
		detailContent : null,
		init : function(){
			KE.show({
				id : ArticlePreview.AddDetail.editorId,
				imageUploadJson : '/admin/image/upload.do',
				fileManagerJson : '/admin/image/listimg.do',
				allowFileManager : true,
				//cssPath : './index.css',
				afterCreate : function(id) {}
			});
			$("#ad-submit").click(function(){
				ArticlePreview.AddDetail.addDetail();
			});
		},
		addDetail : function(){
			if (KE.util.isEmpty(ArticlePreview.AddDetail.editorId)) {
				alert('请输入内容！');
				return;
			}
			$.postJson({
				url: '/admin/articleDetail/add.do',
				data: {
					"articleDetail.articleId" : ArticlePreview.articleId,
					"articleDetail.content" : KE.html(ArticlePreview.AddDetail.editorId)
				},
				success: function(data){
					if(data.result != 0){
						alert("添加成功！");
						window.location.reload();
					} else {
						alert("添加失败！");
					}
				}
			});
		}
	};

	ArticlePreview.EditDetail = function(detailId){
		var editor_id = 'editDetail_' + detailId;
		var articleDetailId = "#articleDetail_"+detailId;
		//编辑器不存在，则先创建编辑器
		if(!$("#"+editor_id)[0]){
			var content = $(articleDetailId).html();
			$(articleDetailId).append("<textarea id='"+editor_id+"' class='articleDetailInput'></textarea>");
			KE.init({
				id : editor_id,
				imageUploadJson : '/admin/image/upload.do',
				fileManagerJson : '/admin/image/listimg.do',
				allowFileManager : true,
				//cssPath : './index.css',
				afterCreate : function(id) {}
			});
			KE.create(editor_id);
			KE.appendHtml(editor_id, content);
			return;
		}
		//保持修改的内容
		if (KE.util.isEmpty(editor_id)) {
			alert('请输入内容！');
			return;
		}
		$.postJson({
			url: '/admin/articleDetail/edit.do',
			data: {
				"articleDetail.articleId" : ArticlePreview.articleId,
				"articleDetail.id" : detailId,
				"articleDetail.content" : KE.html(editor_id)
			},
			success: function(data){
				if(data.result != 0){
					alert("修改成功！");
					window.location.reload();
				} else {
					alert("修改失败！");
				}
			}
		});
	};

	ArticlePreview.DelDetail = function(detailId){
		$.postJson({
			url: '/admin/articleDetail/del.do',
			data: {
				"articleDetail.articleId" : ArticlePreview.articleId,
				"articleDetail.id" : detailId
			},
			success: function(data){
				if(data.result != 0){
					alert("删除成功！");
					window.location.reload();
				} else {
					alert("删除失败！");
				}
			}
		});
	};
	
	$(document).ready(function(){
		ArticlePreview.init();
		ArticlePreview.AddDetail.init();
	});
})(jQuery);